package ar.edu.utn.frba.dds.tp.ui;

import java.text.SimpleDateFormat;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.layout.HorizontalLayout;
import org.uqbar.arena.layout.VerticalLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.tables.Column;
import org.uqbar.arena.widgets.tables.Table;
import org.uqbar.arena.windows.Window;
import org.uqbar.arena.windows.WindowOwner;

import ar.edu.utn.frba.dds.tp.code.transacciones.Transaccion;
import ar.edu.utn.frba.dds.tp.code.usuarios.UsuarioStandard;
import ar.edu.utn.frba.dds.tp.ui.appModel.TransaccionesManager;

import com.uqbar.commons.collections.Transformer;

@SuppressWarnings("serial")
public class TransaccionesWindow extends Window<TransaccionesManager> {

	
	String user;
	String descripcion;

	public TransaccionesWindow(WindowOwner parent, TransaccionesManager model, UsuarioStandard user, String descripcion) {
		super(parent, model);
		this.user = user.getNombre();
		this.descripcion = descripcion;
		this.getModelObject().buscar(user);
	}

	@Override
	public void createContents(Panel mainPanel) {
		this.setTitle("Aterrizar.com");
		mainPanel.setLayout(new VerticalLayout());
		
		this.escribirDescripcion(mainPanel);
		
		this.createResultsGrid(mainPanel);
		
		Panel boton = new Panel(mainPanel).setLayout(new HorizontalLayout());
		new Button(boton) //
		.setCaption("Cerrar")
		.onClick(new MessageSend(this, "close"));
		
	}
	

	protected void escribirDescripcion(Panel mainPanel) {
		Panel descrip = new Panel(mainPanel);
		descrip.setLayout(new HorizontalLayout());
		new Label(descrip).setText(this.descripcion);
		new Label(descrip).setText(this.user);
	}
	
	protected void createResultsGrid(Panel mainPanel) {
		Table<Transaccion> table = new Table<Transaccion>(mainPanel, Transaccion.class);
		table.setHeigth(200);
		table.setWidth(550);

		table.bindItemsToProperty("transacciones");

		this.describeResultsGrid(table);
	}

	protected void describeResultsGrid(Table<Transaccion> table) {
		new Column<Transaccion>(table) //
			.setTitle("Salida")
			.setFixedSize(100)
			.bindContentsToTransformer(new Transformer<Transaccion, String>() {
			@Override
			public String transform(Transaccion t) {
				return new SimpleDateFormat("dd/MM/yyyy").format(t.getFechaSalida());
			}
		});

		new Column<Transaccion>(table) //
			.setTitle("Aerolinea")
			.setFixedSize(100)
			.bindContentsToTransformer(new Transformer<Transaccion, String>() {
			@Override
			public String transform(Transaccion t) {
				return t.getAerolinea().getName();
			}
		});

		new Column<Transaccion>(table) //
			.setTitle("Vuelo")
			.setFixedSize(150)
			.bindContentsToProperty("codigoVuelo");

		new Column<Transaccion>(table) //
		.setTitle("Asiento")
		.setFixedSize(100)
		.bindContentsToTransformer(new Transformer<Transaccion, Integer>() {
			@Override
			public Integer transform(Transaccion element) {
				return element.getAsiento().getNumeroDeAsiento();
			}
		});
//		.bindContentsToProperty("nroAsiento");
		
		new Column<Transaccion>(table) //
		.setTitle("Precio")
		.setFixedSize(100)
		.bindContentsToProperty("precio");
	}

}
